package F21AS_Freddy.AFJ_Taxikiosk.src;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * This a collection class used to collect all passenger.
 * @author Freddy Wong
 * F21AS Course Work Stage2 @ Year 2015
 */

public class PassengerList implements Iterable<Passenger> {

	private TreeSet<Passenger> allPassenger;
	// new line and carriage return escape chars
	private static final String CR_LF = "\r\n";

	/**
	 * Initialize allPassenger
	 */
	public PassengerList() {
		allPassenger = new TreeSet<Passenger>();

	}

	public boolean add(Passenger passenger) {
		if (passenger == null) {
			throw new IllegalArgumentException();
		} else {
			allPassenger.add(passenger);
		}
		return true;
	}

	/**
	 * returns an iterator over the list of passengers
	 * Override
	 */
	public Iterator<Passenger> iterator() {
		return allPassenger.iterator();
	}

	/**
	 * 
	 * @return String containing a list of passengers and their individual
	 *         charges followed by summary information on the total number of
	 *         passengers, the total weight, total volume and total excess
	 *         charges.
	 */
	public String getReport() {
		String report = "";

		Calendar cal = Calendar.getInstance();
		report += ("Date/time report generated: " + cal.getTime() + "\n");
		report += ("Total number of trips are " + allPassenger.size() + "\n");
		report += String.format("%-7s", "Kiosk");
		report += String.format("%-19s", "Booking Reference");
		report += String.format("%-20s", "Customer Name");
		report += String.format("%-20s", "Number of Passenger");
		report += String.format("\n");
		report += "-------------------------------------------------------------------";
		report += String.format("\n");

		for (Passenger p : allPassenger) {

			report += p.getPassengerDetails();
		}

		return report;

	} // End Of getReport 

	/**
	 * remove all passengers from the list
	 */
	public void clear() {
		allPassenger.clear();
	}

	/**
	 * @return true if the list is empty
	 */
	public boolean isEmpty() {
		return allPassenger.isEmpty();
	}

	/**
	 * Write the report into file
	 */
	public void writeToFile(String fileName) {
		try {
			File file = new File(fileName);
			// append the error message to the file
			FileWriter fw = new FileWriter(file, false);
			PrintWriter pw = new PrintWriter(fw);
			pw.print(getReport());
			fw.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

} //End Of Passenger List Class